<?php
namespace backend\controllers;

use Yii;
use backend\controllers\BaseController;
use common\components\CreateExcel;
use yii\data\Pagination;
use yii\web\UploadedFile;
use common\components\Easemob;
use backend\models\user\User;
use backend\models\user\Users;
use backend\models\user\UserLog;
use backend\models\user\Admin;
use backend\models\user\Admins;
use backend\models\user\AuthNode;
use backend\models\user\AuthRole;
use backend\models\user\AuthRoleNode;
use backend\models\user\AuthAdminRole;
use backend\models\user\AuthNodeTemp;
use backend\models\user\TeacherInfo;
use backend\models\user\TeacherPosition;
use backend\models\user\TeacherFrom;
use backend\models\system\SysConfig;

/**
 * 用户功能模块
 * @author 王文秀
 */
class UserController extends BaseController
{
	/**
	 * @inheritdoc  用户管理
	 */
	public function actionIndex()
	{
		$session = Yii::$app->session;
		$user = Yii::$app->request->post();
		@$user_val = $user['user_name'];
		@$user = $user["user"];
		if(!empty(Yii::$app->request->get('page'))){
			$session->open();
			$search=$session->get('search');
			$cond=$session->get('cond');
		}else{
			$search['value'] = $user_val;
			$search['user'] = $user;
			$cond = ['and',['>','user_id','100']];
			if($user == 'uname' && !empty($user_val)){
				$cond[] = ['like','user_name',$user_val];
			}else if($user == 'uphone' && !empty($user_val)){
				$cond[] = ['=','user_phone',$user_val];
			}else if($user == 'uemail' && !empty($user_val)){
				$cond[] = ['=','user_email',$user_val];
			}else if($user == 'uip' && !empty($user_val)){
				$cond[] = ['=','user_lastip',$user_val];
			}
			$session->open();
			$session->set('search', $search);
			$session->set('cond', $cond);
		}
		$model = new Users;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->UserCount($cond),
		]);
		$data = $model -> UserSearch($cond,$pagination);
		/*foreach($data as $key => $val){
			$data[$key]['user_regaddr'] = $model -> getIPLoc_sina($val['user_regip']);
			$data[$key]['user_lastaddr'] = $model -> getIPLoc_sina($val['user_lastip']);
		}*/
	
		return $this->render('index',[
							'data' => $data,
							'model' => $model,
							'pagination' => $pagination,
							'search' => $search,
		]);
	}
	
	/**
	 * @inheritdoc  添加用户
	 */
	public function actionUsercreate(){
		$request = Yii::$app->request;
		$request = $request->post('Users');
		$data['user_regip'] = Yii::$app->request->userIP;//获取ip
		$data['user_regtime'] = time();
		$data['user_name'] = $request['user_name'];
		$data['user_email'] = $request['user_email'];
		$data['user_pwd'] = md5($request['user_pwd']);
		$data['user_emid'] = md5($request['user_pwd']);
		$sysconfig = new SysConfig();
		$sysconfig->config_key = "default_photo";
		$re = $sysconfig->getValueBykey();
		$data['user_thumb'] = $re['config_value'];
		$user = new User;
		$user->attributes = $data;
		if($user -> save()){
			$user_id = $user->db->getLastInsertID();
			$user->user_num='11'.$user_id;
			$user->findOne($user_id);
			$user->save();
			$em = new Easemob();
			$options['username']='11'.$user_id;
			$options['password']=$data['user_emid'];
			$em->openRegister($options);
			$this->success('注册成功!',['user/index']);
		}else{
			$this->error('邮箱重复!',['user/index']);
		}
	}
	
	/**
	 * @inheritdoc  用户详情页
	 */
	public function actionInfo()
	{
		$request = Yii::$app->request;
		$user_id = $request->post("user_id");
		$model = new User;
		$data = $model->User($user_id);
		$data = $this->renderpartial('userinfo',['user' => $data]);
		return $this->ajaxSuccess($data);
	}
	/**
	 * @inheritdoc  修改用户状态
	 */
	public function actionStatus()
	{
		$request = Yii::$app->request;
		$user_id = $request->post("user_id");
		$user_status = $request->post("user_status");
		$model = new User;
		$res = $model->UserStatus($user_id,$user_status);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}
	/**
	 * @inheritdoc  用户登录日志
	 
	public function actionLoginlog(){
		return $this->render('loginlog');
	}
    */

	/**
	 * @inheritdoc  教师来源管理
	 */
	public function actionFrom()
	{
		$model = new TeacherFrom;
		$data = $model -> FromSel();
		return $this->render('from',[
						'data' => $data,
						'model' => $model,
		]);
	}
	
	/**
	 * @inheritdoc  添加来源
	 */
	public function actionFromcreate()
	{
		$model = new TeacherFrom;
		if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$this->success('添加来源成功!',['user/from']);
		} else {
			$this->error('添加来源失败!',['user/from']);
		}
	}

	/**
	 * @wwx  删除来源
	 */
	public function actionFromdelete()
	{
		$request = Yii::$app->request;
		$from_id = $request->post("from_id");
		$model = new TeacherFrom;
		$CourseSource = $model->findOne($from_id);
		$res=$CourseSource->delete();
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}
	/**
	 * @wwx  修改显示教师来源
	 */
	public function actionFromupdate()
	{
		$request = Yii::$app->request;
		$model = new TeacherFrom;
		if($request->post('type') == 'list'){
			$from_id = $request->post("from_id");
			$model = $model->findOne($from_id);
			$data = $this->renderPartial('fromupdate', ['model' => $model,]);
			return $this->ajaxSuccess($data);
		}else{
			$TeacherFrom = $request->post("TeacherFrom");
			$from_id = $TeacherFrom['from_id'];
			$model = $model->findOne($from_id);
			if ($model->load($request->post()) && $model->save()) {
				$this->success('修改职位成功!',['user/from']);
			} else {
				$this->error('修改职位失败',['user/from']);
			}
		}
		
	}

	/**
	 * @inheritdoc  教师管理
	 */
	public function actionTeacher()
	{
		$model = new Admins;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->TeacherCount(),
		]);
		$data = $model -> TeacherSearch($pagination);
		return $this->render('teacher',[
						'pagination' => $pagination,
						'model' => $model,
						'data' => $data,
				]);
	}

	/**
	 * @inheritdoc  添加新教师管理
	 */
	public function actionTeachercreate()
	{
		$request = Yii::$app->request;
		$request = $request->post('Admins');
		$data['username'] = $request['username'];
		$data['password_hash'] = Yii::$app->getSecurity()->generatePasswordHash($request['password_hash']);//加密密码
		$data['email'] = $request['email'];
		$data['created_at'] = time();
		$data['updated_at'] = time();
		$data['auth_key'] = '123456';
		$data['role'] = '10';
		$data['status'] = '10';
		$admin = new admin;
		$admin -> attributes = $data;
		if($admin -> save()){
			$sysconfig = new SysConfig();
			$sysconfig->config_key = "default_photo";
			$re = $sysconfig->getValueBykey();
			$id = $admin->db->getLastInsertID();
			$arr['teacher_id'] = $id;
			$arr['teacher_name'] = $request['teacher_name'];
			$arr['teacher_thumb'] = $re['config_value'];
			$teacher = new TeacherInfo;
			$teacher -> attributes = $arr;
			if($teacher -> save()){
				$this->success('添加教师成功!',['user/teacher']);
			}
		} else {
			$this->error('用户名或是邮箱重复!',['user/teacher']);
		}
	}
	
	/**
	 * @inheritdoc  修改推荐
	 */
	public function actionRecommend(){
		$request = Yii::$app->request;
		$id = $request->post("id");
		$status = $request->post("status");
		$model = new TeacherInfo;
		$res = $model->TeacherInfoRecommend($id,$status);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}

	/**
	 * @inheritdoc  教师详情页
	 */
	public function actionTeacherinfo()
	{
		$request = Yii::$app->request;
		$id = $request->post('id');
		$model = new Admin;
		$data = $model->Teacher($id);
		//教师来源
		if($data['from_id']){
			$teacher_form = new TeacherFrom;
			$from = $teacher_form -> FromFind($data['from_id']);
			$data['from_id'] = $from['from_name'];
		}
		//教师职位
		if($data['position_id']){
			$teacher_position = new TeacherPosition;
			$position = $teacher_position -> PositionFind($data['position_id']);
			$data['position_id'] = $position['position_name'];
		}
		$data = $this->renderpartial('teacherinfo',['teacher' => $data]);
		return $this->ajaxSuccess($data);
	}
	
	/**
	 * @inheritdoc  教师信息完善
	 */
	public function actionTeachersave()
	{
		$request = Yii::$app->request;
		if($request->post('type') == 'list'){
			$id = $request->post("id");
			$admins = new Admins;
			$data = $admins->Teacher($id);
			//职位
			$teacher_position = new TeacherPosition;
			$positions = $teacher_position -> Position();
			foreach($positions as $key => $val)
			{
				$position[$val['position_id']] = $val['position_name'];
			}
			//来源
			$teacher_from = new TeacherFrom;
			$froms = $teacher_from -> From();
			foreach($froms as $key => $val)
			{
				$from[$val['from_id']] = $val['from_name'];
			}
			$model = new TeacherInfo;
			$model = $model -> findOne($id);
			$arr = $this->renderpartial('teachersave',[
								'model' => $model,
								'teacher' => $data,
								'position' => $position,
								'from' => $from,
			]);
			return $this->ajaxSuccess($arr);
		}else{
			$request = $request->post();
			$teacher_id = $request['TeacherInfo']['teacher_id'];
			$model = new TeacherInfo;
			$model = $model->findOne($teacher_id);
			if ($model->load($request)) {
				if($model->save()){
					//修改邮箱
					$admin = new Admin;
					$email = $request['TeacherInfo']['email'];
					$admin -> EmailSave($teacher_id,$email);
					$this->success('完善教师信息成功!',['user/teacher']);
				}else{
					$this->error('完善教师信息失败!',['user/teacher']);
				}
			}
		}
	}
	/**
	 * @inheritdoc  教师职位管理
	 */
	public function actionPosition()
	{
		$model = new TeacherPosition;
		$data = $model -> PositionSel();
		return $this->render('position',[
						'data' => $data,
						'model' => $model,
		]);
	}
	/**
	 * @inheritdoc  添加教师职位
	 */
	public function actionPositioncreate()
	{
		$model = new TeacherPosition;
		if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$this->success('添加职位成功!',['user/position']);
		} else {
			$this->error('添加职位失败!',['user/position']);
		}
	}

	/**
	 * @wwx  删除教师职位
	 */
	public function actionPositiondelete()
	{
		$request = Yii::$app->request;
		$position_id = $request->post("position_id");
		$model = new TeacherPosition;
		$CourseSource = $model->findOne($position_id);
		$res=$CourseSource->delete();
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}
	/**
	 * @wwx  修改显示教师职位
	 */
	public function actionPositionupdate()
	{
		$request = Yii::$app->request;
		$model = new TeacherPosition;
		if($request->post('type') == 'list'){
			$position_id = $request->post("position_id");
			$model = $model->findOne($position_id);
			$data = $this->renderPartial('positionupdate', ['model' => $model,]);
			return $this->ajaxSuccess($data);
		}else{
			$TeacherPosition = $request->post("TeacherPosition");
			$position_id = $TeacherPosition['position_id'];
			$model = $model->findOne($position_id);
			if ($model->load($request->post()) && $model->save()) {
				$this->success('修改职位成功!',['user/position']);
			} else {
				$this->error('修改职位失败!',['user/position']);
			}
		}
		
	}
	/**
	 * @inheritdoc  管理员管理
	 */
	public function actionAdmin()
	{
		$session = Yii::$app->session;
		$admin = Yii::$app->request->post();
		@$admin_value = $admin['admin_value'];
		@$admin = $admin["admin"];
		if(!empty(Yii::$app->request->get('page'))){
			$session->open();
			$search=$session->get('search');
			$cond=$session->get('cond');
		}else{
			$search['value'] = $admin_value;
			$search['admin'] = $admin;
			$cond = ['and',['>','id','1']];
			if($admin == 'aname' && !empty($admin_value)){
				$cond[] = ['like','username',$admin_value];
			}else if($admin == 'aemail' && !empty($admin_value)){
				$cond[] = ['=','email',$admin_value];
			}else if($admin == 'aip' && !empty($admin_value)){
				$cond[] = ['=','login_ip',$admin_value];
			}
			$session->open();
			$session->set('search', $search);
			$session->set('cond', $cond);
		}
		$model = new Admins;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->AdminCount($cond),
		]);
		$data = $model -> AdminSearch($cond,$pagination);
		return $this->render('admin',[
							'data' => $data,
							'model' => $model,
							'pagination' => $pagination,
							'search' => $search,
		]);
	}

	/**
	 * @inheritdoc  修改管理员状态
	 */
	public function actionAdminstatus()
	{
		$request = Yii::$app->request;
		$id = $request->post("id");
		$status = $request->post("status");
		$model = new Admin;
		$res = $model->AdminStatus($id,$status);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}
	
	/**
	 * @inheritdoc  管理员详情页
	 */
	public function actionAdmininfo()
	{
		$request = Yii::$app->request;
		$id = $request->post("id");
		$model = new Admin;
		$data = $model->AdminInfo($id);
		$auth_admin_role = new AuthAdminRole;
		$admin_role = $auth_admin_role->AdminRole($id);
		$auth_role = new AuthRole;
		$role = $auth_role -> RoleSelect();
		//判断该角色有哪些角色
		foreach ($role as $k => $v) {
			$role[$k]['is_checked'] = 0;
			foreach ($admin_role as $kk => $vv) {
				if ($v['role_id'] == $vv['role_id']) {
					$role[$k]['is_checked'] = 1;
				}
			}
		}
		$arr = $this->renderpartial('admininfo',[
							'admin' => $data,
							'role' => $role,
		]);
		return $this->ajaxSuccess($arr);
	}
	
	
	/**
	 * @inheritdoc  添加管理角色
	 */
	public function actionAdminrolecreate()
	{
		$request = Yii::$app->request->post();
		$role_id = $request['role_id'];
		$admin_id = $request['admin_id'];
		foreach($role_id as $key => $val)
		{
			$data[$key]['role_id'] = $val;
			$data[$key]['admin_id'] = $admin_id;
		}
		//判断
		$auth_admin_role = new AuthAdminRole;
		$admin_role = $auth_admin_role->AdminRole($admin_id);
		if($admin_role){
			//批量删除
			$re = $auth_admin_role -> deleteAll(['=','admin_id',$admin_id]);
			if($re){
				//批量添加
				foreach($data as $attributes)
				{
					$_model = clone $auth_admin_role;
					$_model->setAttributes($attributes);
					$_model->save();
				}
				$this->success('添加角色成功!',['user/admin']);
			}
		}else{
			//批量添加
			foreach($data as $attributes)
			{
				$_model = clone $auth_admin_role;
				$_model->setAttributes($attributes);
				$_model->save();
			}
			$this->success('添加角色成功!',['user/admin']);
		}
	}

	/**
	 * @inheritdoc  管理员分配角色
	 */
	public function actionRoleallot()
	{
		$request = Yii::$app->request;
		$id = $request->post("id");
		$model = new Admin;
		$data = $model->AdminInfo($id);
		$auth_admin_role = new AuthAdminRole;
		$admin_role = $auth_admin_role->AdminRole($id);
		$auth_role = new AuthRole;
		$role = $auth_role -> RoleSelect();
		//判断该角色有哪些角色
		foreach ($role as $k => $v) {
			$role[$k]['is_checked'] = 0;
			foreach ($admin_role as $kk => $vv) {
				if ($v['role_id'] == $vv['role_id']) {
					$role[$k]['is_checked'] = 1;
				}
			}
		}
		
		//获取该用户下的所有
		$arr = $this->renderpartial('roleallot',[
						'admin' => $data,
						'role' => $role,
						'admin_id' => $id,
		]);
		return $this->ajaxSuccess($arr);
	}
	
	/**
	 * @inheritdoc  添加管理员
	 */
	public function actionAdmincreate()
	{
		$request = Yii::$app->request;
		$request = $request->post('Admins');
		$data['username'] = $request['username'];
		$data['password_hash'] = Yii::$app->getSecurity()->generatePasswordHash($request['password_hash']);//加密密码
		$data['email'] = $request['email'];
		$data['created_at'] = time();
		$data['updated_at'] = time();
		$data['auth_key'] = '123456';
		$data['role'] = '10';
		$data['status'] = '10';
		$admin = new admin;
		$admin -> attributes = $data;
		if($admin -> save()){
			$this->success('添加管理员成功!',['user/admin']);
		} else {
			$this->error('用户名或是邮箱重复!',['user/admin']);
		}
	}

	/**
	 * @inheritdoc 角色管理
	 */
	public function actionRole()
	{

		$session = Yii::$app->session;
		$role = Yii::$app->request->post();
		@$role_value = $role['role_value'];
		@$role = $role["role"];
		if(!empty(Yii::$app->request->get('page'))){
			$session->open();
			$search=$session->get('search');
			$cond=$session->get('cond');
		}else{
			$search['value'] = $role_value;
			$search['role'] = $role;
			$cond = ['and',['>','role_id','0']];
			if($role == 'rname' && !empty($role_value)){
				$cond[] = ['like','role_name',$role_value];
			}else if($role == 'aremark' && !empty($role_value)){
				$cond[] = ['like','role_remark',$role_value];
			}
			$session->open();
			$session->set('search', $search);
			$session->set('cond', $cond);
		}
		$model = new AuthRole;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->RoleCount($cond),
		]);
		$data = $model -> RoleSearch($cond,$pagination);
		return $this->render('role',[
							'data' => $data,
							'model' => $model,
							'pagination' => $pagination,
							'search' => $search,
		]);
	}

	/**
	 * @wwx  添加角色
	 */
	public function actionRolecreate()
	{
		$model = new AuthRole;
		if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$this->success('添加角色成功!',['user/role']);
		} else {
			$this->error('添加角色失败!',['user/role']);
		}
	}
	
	/**
	 * @inheritdoc  修改角色状态
	 */
	public function actionRolestatus()
	{
		$request = Yii::$app->request;
		$id = $request->post("id");
		$status = $request->post("status");
		$model = new AuthRole;
		$res = $model->RoleStatus($id,$status);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}


	/**
	 * @wwx  修改显示角色
	 */
	public function actionRoleupdate()
	{
		$request = Yii::$app->request;
		$model = new AuthRole;		
		if($request->post('type') == 'list'){
			$role_id = $request->post("role_id");
			$model = $model->findOne($role_id);
			$data = $this->renderPartial('roleupdate', ['model' => $model,]);
			return $this->ajaxSuccess($data);
		}else{
			$AuthRole = $request->post("AuthRole");
			$role_id = $AuthRole['role_id'];
			$model = $model->findOne($role_id);
			if ($model->load($request->post()) && $model->save()) {
				$this->success('修改角色成功!',['user/role']);
			} else {
				$this->error('修改角色失败!',['user/role']);
			}
		}
	}
	
	/**
	 * @inheritdoc  删除角色
	 */
	public function actionRoledelete()
	{
		$request = \YII::$app->request;
		$role_id = $request->post("role_id");
		$model = new AuthRole;
		$auth_role = $model->findOne($role_id);
		$res=$auth_role->delete();
		if($res){
			$auth_role_node = new AuthRoleNode;
			$re = $auth_role_node -> deleteAll(['=','role_id',$role_id]);
			if($re){
				return $this->ajaxSuccess(1);
			}else{
				return $this->ajaxError(0);
			}
		}else{
			return $this->ajaxError(0);
		}
	}
	/**
	 * @inheritdoc  角色分配
	 */
	public function actionAuthallot(){
		$request = Yii::$app->request;
		$role_id = $request->post("role_id");
		$model = new AuthRole;
		$role = $model->findOne($role_id);
		//获取节点
		$auth_node = new AuthNode;
		$auth = $auth_node -> AuthSelect();
		//获取当前角色可操作节点
		$AuthRoleNode = new AuthRoleNode;
		$role_node = $AuthRoleNode -> RoleNode($role_id);
		//判断该角色有哪些权限
		foreach ($auth as $k => $v) {
			$auth[$k]['is_checked'] = 0;
			foreach ($role_node as $kk => $vv) {
				if ($v['node_id'] == $vv['node_id']) {
					$auth[$k]['is_checked'] = 1;
				}
				
			}
		}
		//处理权限节点
		foreach($auth as $key => $val){
			if($val['node_pid'] == 0){
				$arr[] =  $val;
			}
			foreach($arr as $k => $v){
				if($v['node_id'] == $val['node_pid']){
					$arr[$k]['node'][] =$val;
				}
			}
		}
		$data = $this->renderPartial('authallot', [
							'model' => $auth_node,
							'role' => $role,
							'auth' => $arr,
							'role_id' => $role_id,
			]);
		return $this->ajaxSuccess($data);
	}
	/**
	 * @inheritdoc  角色添加权限
	 */
	public function actionRolenodecreate(){
		$request = Yii::$app->request->post();
		$role_id = $request['role_id'];
		$node_id = $request['node_id'];
		foreach($node_id as $key => $val)
		{
			$data[$key]['role_id'] = $role_id;
			$data[$key]['node_id'] = $val;
		}
		//判断
		$auth_role_node = new AuthRoleNode;
		$role_node = $auth_role_node -> RoleNode($role_id);
		if($role_node){
			//批量删除
			$re = $auth_role_node -> deleteAll(['=','role_id',$role_id]);
			if($re){
				//批量添加
				foreach($data as $attributes)
				{
					$_model = clone $auth_role_node;
					$_model->setAttributes($attributes);
					$_model->save();
				}
				$this->success('添加权限成功!',['user/role']);
			}
		}else{
			//批量添加
			foreach($data as $attributes)
			{
				$_model = clone $auth_role_node;
				$_model->setAttributes($attributes);
				$_model->save();
			}
			$this->success('添加权限成功!',['user/role']);
		}
	}
	
	/**
	 * @inheritdoc  权限分配
	 */
	public function actionRoleinfo(){
		$request = Yii::$app->request;
		$role_id = $request->post("role_id");
		$model = new AuthRole;
		$role = $model->findOne($role_id);
		//获取节点
		$auth_node = new AuthNode;
		$auth = $auth_node -> AuthSelect();
		//获取当前角色可操作节点
		$AuthRoleNode = new AuthRoleNode;
		$role_node = $AuthRoleNode -> RoleNode($role_id);
		//判断该角色有哪些角色
		foreach ($auth as $k => $v) {
			$auth[$k]['is_checked'] = 0;
			foreach ($role_node as $kk => $vv) {
				if ($v['node_id'] == $vv['node_id']) {
					$auth[$k]['is_checked'] = 1;
				}
			}
			
		}
		//处理权限节点
		foreach($auth as $key => $val){
			if($val['node_pid'] == 0){
				$arr[] =  $val;
			}
			foreach($arr as $k => $v){
				if($v['node_id'] == $val['node_pid']){
					$arr[$k]['node'][] =$val;
				}
			}
		}
		$data = $this->renderPartial('roleinfo', [
							'model' => $auth_node,
							'role' => $role,
							'auth' => $arr,
							'role_id' => $role_id,
			]);
		return $this->ajaxSuccess($data);
	}
	
	/**
	 * @inheritdoc  权限管理
	 */
	public function actionAuth()
	{
		$session = Yii::$app->session;
		$auth = Yii::$app->request->post();
		@$auth_value = $auth['auth_value'];
		@$auth = $auth["auth"];
		if(!empty(Yii::$app->request->get('page'))){
			$session->open();
			$search=$session->get('search');
			$cond=$session->get('cond');
		}else{
			$search['value'] = $auth_value;
			$search['auth'] = $auth;
			$cond = ['and',['>','node_id','0']];
			if($auth == 'node_name' && !empty($auth_value)){
				$cond[] = ['like','node_name',$auth_value];
			}else if($auth == 'node_title' && !empty($auth_value)){
				$cond[] = ['like','node_title',$auth_value];
			}
			$session->open();
			$session->set('search', $search);
			$session->set('cond', $cond);
		}
		$model = new AuthNode;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->AuthCount($cond),
		]);
		$data = $model -> AuthSearch($cond,$pagination);
		return $this->render('auth',[
							'data' => $data,
							'model' => $model,
							'pagination' => $pagination,
							'search' => $search,
		]);
	}

	/**
	 * @inheritdoc  修改权限状态
	 */
	public function actionAuthstatus()
	{
		$request = Yii::$app->request;
		$id = $request->post("id");
		$status = $request->post("status");
		$model = new AuthNode;
		$res = $model->AuthStatus($id,$status);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}
	/**
	 * @inheritdoc  临时权限
	 */
	public function actionTemporaryauth()
	{
		//判断临时权限是否过期
		$auth_node_temp = new AuthNodeTemp;
		$code = ['<','node_temp_expire',time()];
		$auth_node_temp -> TempDelete($code);
		//获取当前用户id
		$user_id = Yii::$app->user->id;
		$node_temp = $auth_node_temp -> TempUser($user_id);
		if($node_temp){
			$node_temp = array_unique($node_temp);
			foreach($node_temp as $key => $val){
				$admin_arr[$k] = $val['node_temp_admin'];
			}
			$admin_id = implode(',',$admin_arr);
			$admin = new Admin;
			$admin_user = $admin -> AdminSelect("id in ($admin_id)");
		}else{
		    $admin_user='';
		}
		$auth_node = new AuthNode;
		//所有管理员
		$admin = new Admin;
		$code = ['and',['!=','username','admin'],['=','status','10'],['!=','id',"$user_id"]];
		$admin_data = $admin -> AdminSelect($code);
		$admin_datas[''] = "--请选择用户--";
		foreach($admin_data as $key => $val)
		{
			$admin_datas[$val['id']] = $val['username'];
		}
		//当前用户数据
		$admin_info = $admin -> AdminInfo("$user_id");
		if($admin_info['username'] == "admin"){
			//所有权限
			$auth = $auth_node -> AuthSelect();
			foreach($auth as $key => $val){
				if($val['node_pid'] == 0){
					$arr[] =  $val;
				}
				foreach($arr as $k => $v){
					if($v['node_id'] == $val['node_pid']){
						$arr[$k]['node'][] =$val;
					}
				}
			}
		}else{
			$auth_admin_role = new AuthAdminRole;
			$admin_role = $auth_admin_role->AdminRole("$user_id");
			if($admin_role){
				//获取角色id
				$role_id = null;
				foreach($admin_role as $key => $val)
				{
					$role_id[] = $val['role_id'];
				}
				if($role_id){
					$role_id = implode(',',$role_id);
				}
				//获取权限id
				$auth_role_node = new AuthRoleNode;
				$node = $auth_role_node -> RoleNodeIn($role_id);
				foreach($node as $key => $val)
				{
					$node_id[$key] = $val['node_id'];
				}
				$node_id = array_unique($node_id);
				$node_id = implode(',',$node_id);
				//所有权限
				$auth = $auth_node -> AuthIn($node_id);
				foreach($auth as $key => $val){
					if($val['node_pid'] == 0){
						$arr[] =  $val;
					}
					foreach($arr as $k => $v){
						if($v['node_id'] == $val['node_pid']){
							$arr[$k]['node'][] =$val;
						}
					}
				}
			}
		}
		return $this->render('temporaryauth',[
						'admin' => $admin_user,
						'admin_info' => $admin_info,
						'admin_data' => $admin_datas,
						'auth' => $arr,
		]);
	}
	
	/**
	 * @inheritdoc  添加临时权限
	 */
	public function actionTemporarycreate()
	{
		$request = Yii::$app->request->post();
		if($request['node_id'] && $request['admin_id'] && $request['node_temp_expire']){
			$node_id = implode(',',$request['node_id']);
			$auth_node = new AuthNode;
			$node = $auth_node -> AuthIn($node_id);
			foreach($node as $key => $val){
				$arr[$key]['node_id'] = $val['node_id'];
				$arr[$key]['node_temp_name'] = $val['node_name'];
				$arr[$key]['node_temp_title'] = $val['node_title'];
				$arr[$key]['node_temp_status'] = $val['node_status'];
				$arr[$key]['node_temp_remark'] = $val['node_remark'];
				$arr[$key]['node_temp_sort'] = $val['node_sort'];
				$arr[$key]['node_temp_pid'] = $val['node_pid'];
				$arr[$key]['node_temp_level'] = $val['node_level'];
				$arr[$key]['node_temp_admin'] = $request['admin'];
				$arr[$key]['node_temp_expire'] = strtotime($request['node_temp_expire']);
				$arr[$key]['node_temp_share_id'] = $request['admin_id'];
			}
			$auth_node_temp = new AuthNodeTemp;
			foreach($arr as $attributes)
			{
				$_model = clone $auth_node_temp;
				$_model->setAttributes($attributes);
				$_model->save();
			}
			$this->success('添加权限成功!',['user/temporaryauth']);
		}else{
			$this->error('权限,授权人,结束时间不能为空!',['user/temporaryauth']);
		}
	}

	/**
	 * @inheritdoc  临时授权详情
	 */
	public function actionTempallot()
	{
		$request = Yii::$app->request->post();
		$id = $request['id'];
		$admin = new Admin;
		//获取当前用户id
		$user_id = Yii::$app->user->id;
		$admin_info = $admin -> AdminInfo("$user_id");
		$admin = $admin -> AdminInfo($id);
		$auth_node = new AuthNode;
		$auth_node_temp = new AuthNodeTemp;
		$temp_node = $auth_node_temp -> TempNode($id);
		if($admin_info['username'] == "admin"){
			//所有权限
			$auth = $auth_node -> AuthSelect();
			foreach ($auth as $k => $v) {
				$auth[$k]['is_checked'] = 0;
				foreach ($temp_node as $kk => $vv) {
					if ($v['node_id'] == $vv['node_id']) {
						$auth[$k]['is_checked'] = 1;
					}
				}
			}
			foreach($auth as $key => $val){
				if($val['node_pid'] == 0){
					$arr[] =  $val;
				}
				foreach($arr as $k => $v){
					if($v['node_id'] == $val['node_pid']){
						$arr[$k]['node'][] =$val;
					}
				}
			}
		}else{
			$auth_admin_role = new AuthAdminRole;
			$admin_role = $auth_admin_role->AdminRole("$user_id");
			if($admin_role){
				//获取角色id
				$role_id = null;
				foreach($admin_role as $key => $val)
				{
					$role_id[] = $val['role_id'];
				}
				if($role_id){
					$role_id = implode(',',$role_id);
				}
				//获取权限id
				$auth_role_node = new AuthRoleNode;
				$node = $auth_role_node -> RoleNodeIn($role_id);
				foreach($node as $key => $val)
				{
					$node_id[$key] = $val['node_id'];
				}
				$node_id = array_unique($node_id);
				$node_id = implode(',',$node_id);
				//所有权限
				$auth = $auth_node -> AuthIn($node_id);
				foreach ($auth as $k => $v) {
					$auth[$k]['is_checked'] = 0;
					foreach ($temp_node as $kk => $vv) {
						if ($v['node_id'] == $vv['node_id']) {
							$auth[$k]['is_checked'] = 1;
						}
					}
				}
				foreach($auth as $key => $val){
					if($val['node_pid'] == 0){
						$arr[] =  $val;
					}
					foreach($arr as $k => $v){
						if($v['node_id'] == $val['node_pid']){
							$arr[$k]['node'][] =$val;
						}
					}
				}
			}
		}
		//获取查询临时权限用户的权限
		$temp_node = array_unique($temp_node);
		$data = $this->renderPartial('tempallot', [
							'admin' => $admin,
							'auth' => $arr,
							'temp_node' => $temp_node,
							'admin_data' => $admin_data,
			]);
		return $this->ajaxSuccess($data);
	}
	
	/**
	 * @inheritdoc  收回临时权限
	 */
	public function actionTempdelete()
	{
		$request = Yii::$app->request->post();
		$id = $request['id'];
		$auth_node_temp = new AuthNodeTemp;
		$code = ['=','node_temp_admin',$id];
		$res = $auth_node_temp -> TempDelete($code);
		if($res){
			return $this->ajaxSuccess(1);
		}else{
			return $this->ajaxError(0);
		}
	}

	/**
	 * @inheritdoc  批量导入
	 */
	public function actionImport()
	{
		return $this->render('import');
	}
	/**
	 * @inheritdoc  导出用户列表
	 */
	public function actionExport()
	{
		$user = Yii::$app->request->post();
		$user_val = $user['User']['user_name'];
		$user = $user["user"];
		//搜索条件
		$cond = ['and',['>','user_id','100']];
		if($user == 'uname' && !empty($user_val)){
			$cond[] = ['like','user_name',$user_val];
		}else if($user == 'uphone' && !empty($user_val)){
			$cond[] = ['=','user_phone',$user_val];
		}else if($user == 'uemail' && !empty($user_val)){
			$cond[] = ['=','user_email',$user_val];
		}else if($user == 'uip' && !empty($user_val)){
			$cond[] = ['=','user_lastip',$user_val];
		}
		$model = new User;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->UserCount($cond),
		]);
		$data = $model -> UserSearch($cond,$pagination);
		foreach($data as $key => $val){
			$arr[$key]['user_name']=$val['user_name'];
			$arr[$key]['user_phone']=$val['user_phone'];
			$arr[$key]['user_email']=$val['user_email'];
			$arr[$key]['user_regip']=$val['user_regip'];
			$arr[$key]['user_regtime']=date("Y-m-d H:i:s",$val['user_time']);
			$arr[$key]['user_lastip']=$val['user_lastip'];
			$arr[$key]['user_lastlogin']=date("Y-m-d H:i:s",$val['user_lastlogin']);
		}
		if (!$arr){
			echo "<script>alert('您要导出的数据不存在！');location.href='/index.php?r=user/index'</script>";die;
		}

		//生成excel
		$excel = new CreateExcel();
		$excel->createByArray($arr,'用户结果列表',['用户名','手机号','email','注册ip','注册时间','登录ip','登录时间']);
	}
	/**
	 * @inheritdoc  导出管理员列表
	 */
	public function actionAdminexport()
	{
		$admin = Yii::$app->request->post();
		$admin_value = $admin['admin_value'];
		$admin = $admin["admin"];
		$search['value'] = $admin_value;
		$search['admin'] = $admin;
		$cond = ['and',['>','id','1']];
		if($admin == 'aname' && !empty($admin_value)){
			$cond[] = ['like','username',$admin_value];
		}else if($admin == 'aemail' && !empty($admin_value)){
			$cond[] = ['=','email',$admin_value];
		}else if($admin == 'aip' && !empty($admin_value)){
			$cond[] = ['=','login_ip',$admin_value];
		}
		$model = new Admins;
		$pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $model->AdminCount($cond),
		]);
		$data = $model -> AdminSearch($cond,$pagination);
		foreach($data as $key => $val){
			$arr[$key]['username']=$val['username'];
			$arr[$key]['email']=$val['email'];
			$arr[$key]['created_at']=date("Y-m-d H:i:s",$val['created_at']);
			$arr[$key]['login_ip']=$val['login_ip'];
			$arr[$key]['login_time']=date("Y-m-d H:i:s",$val['login_time']);
		}
		if (!$arr){
			echo "<script>alert('您要导出的数据不存在！');location.href='/index.php?r=user/admin'</script>";die;
		}

		//生成excel
		$excel = new CreateExcel();
		$excel->createByArray($arr,'管理员结果列表',['用户名','email','注册时间','登录ip','登录时间']);
	}
}
